//
//  DHDiagnosisViewController.m
//  DHKuaiYi
//
//  Created by xiaohuihui on 2016/11/13.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHDiagnosisViewController.h"
#import "DHSubtitleView.h"
#import "DHWeatherView.h"
#import "DHDoctorView.h"
#import "Masonry.h"
#import "SDCycleScrollView.h"
#import "NetworkTool.h"
#import "DHWeatherModel.h"
#import "DHChooseDiseaseViewController.h"
#import "DHDoctorCollectionViewCell.h"
#import <YYModel.h>
#import <UIViewController+MMDrawerController.h>
#import "DHPublicActivitiesViewController.h"

@interface DHDiagnosisViewController ()<SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

/* 天气 */
@property (strong, nonatomic) DHWeatherView *weatherView;
/* 图片轮播 */
@property (strong, nonatomic) SDCycleScrollView *cycleScrollView;
/* 字幕栏 */
@property (strong, nonatomic) DHSubtitleView *subtitleView;
/* 名医通九宫格 */
@property (strong, nonatomic) DHDoctorView *doctorView;
/* 图片数组 */
@property (strong, nonatomic) NSArray *arr;
/*名医通名称数组 */
@property (strong, nonatomic) NSArray *nameArr;
/*模型*/
@property (strong, nonatomic) DHWeatherModel *weatherModel;
/* 跑马灯 */
@property (strong, nonatomic) UIScrollView *loopView;

@end

@implementation DHDiagnosisViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    [self getloopData];
    [self getWeatherData];
}

/**
    初始化
 */
- (void)xhhSetupSubviews {
    
    //天气
    DHWeatherView *weatherView = [[DHWeatherView alloc] init];
    [self.view addSubview:weatherView];
    
    //图片轮播
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];
    cycleScrollView.autoScrollTimeInterval = 2.0;
    [self.view addSubview:cycleScrollView];
    
    //字幕栏
    DHSubtitleView *subtitleView = [[DHSubtitleView alloc] init];
    [self.view addSubview:subtitleView];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //名医通九宫格
    DHDoctorView *doctorView = [[DHDoctorView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    
    //跑马灯
    /*---------- 跑马灯视图 ----------*/
    UIScrollView *loopView = [[UIScrollView alloc] init];
    [self.view insertSubview:loopView belowSubview:subtitleView];
    
    //代理设置
    doctorView.delegate=self;
    doctorView.dataSource=self;
    self.doctorView = doctorView;
    //注册item类型
    [doctorView registerClass:[DHDoctorCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:doctorView];
   
    //自动布局
    /*---------- 适配的高度计算 ----------*/
    
    //算出比例(与宽度做对比)
    CGFloat weatherViewSizeScale     = (float)70 / 667;
    CGFloat cycleScrollViewSizeScale = (float)250 / 667;
    CGFloat subtitleViewSizeScale    =  (float)45 / 667;
    
    //view的高度
    CGFloat weatherViewHeight = viewHeight * weatherViewSizeScale;
    CGFloat cycleScrollViewHeight = viewHeight * cycleScrollViewSizeScale;
    CGFloat subtitleViewHeight = viewHeight * subtitleViewSizeScale;
    
    //天气
    [weatherView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(weatherViewHeight);
    }];
    
    //图片轮播
    [cycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weatherView.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(cycleScrollViewHeight);
    }];
    
    //字幕栏
    [subtitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cycleScrollView.mas_bottom);
        make.left.mas_equalTo(self.view);
        make.height.mas_equalTo(subtitleViewHeight);
        make.width.mas_equalTo(90);
    }];
    
    //跑马灯
    [loopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(cycleScrollView.mas_bottom);
        make.left.mas_equalTo(subtitleView.mas_right);
        make.right.mas_equalTo(self.view);
        make.height.mas_equalTo(subtitleViewHeight);

    }];
    
    //名医通九宫格
    [doctorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(subtitleView.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
    }];
    
    // 记录控件
    self.cycleScrollView = cycleScrollView;
    self.subtitleView = subtitleView;
    self.weatherView = weatherView;
    self.doctorView = doctorView;
    self.loopView = loopView;
}

/**
 约束已经转换成了frame
 */
- (void)viewDidLayoutSubviews {
    
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.doctorView.collectionViewLayout;
    
    CGSize itemSize = CGSizeMake(self.doctorView.frame.size.width / 3, self.doctorView.frame.size.height / 2);
    flowLayout.itemSize =itemSize;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;

}
-(void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    
    /*---------- 添加边界线 ----------*/
    UIView *widthLineOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, 1)];
    widthLineOne.backgroundColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];
    [self.doctorView addSubview:widthLineOne];
    
    UIView *widthLineTwo = [[UIView alloc] initWithFrame:CGRectMake(0, self.doctorView.xhh_height / 2, viewWidth, 1)];
    widthLineTwo.backgroundColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];;
    [self.doctorView addSubview:widthLineTwo];
    
    UIView *widthLineThree = [[UIView alloc] initWithFrame:CGRectMake(self.doctorView.xhh_width / 3, 0, 1, self.doctorView.xhh_height)];
    widthLineThree.backgroundColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];;
    [self.doctorView addSubview:widthLineThree];
    
    UIView *widthLineFour = [[UIView alloc] initWithFrame:CGRectMake(2 * self.doctorView.xhh_width / 3, 0, 1, self.doctorView.xhh_height)];
    widthLineFour.backgroundColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];;
    [self.doctorView addSubview:widthLineFour];
    
    /*---------- 跑马灯动画 ----------*/
    NSString *text = @"组名 : Coders of Dark Horsemen 组长 :  陈汉茂  陈辉华  郑育彬  刘鹏飞  韩旭";
    UILabel *runLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 700, 25)];
    runLabel.text = text;
    runLabel.textColor = [UIColor globalColor];
    [self.loopView addSubview:runLabel];
    
    CGRect frame = runLabel.frame;
    frame.origin.x = self.view.frame.size.width-100;
    runLabel.frame = frame;
    
    [UIView beginAnimations:nil context:NULL];
    //动画时间（参数需要按需调整）
    [UIView setAnimationDuration:frame.size.width/50.0f];
    //无限重复
    [UIView setAnimationRepeatCount:CGFLOAT_MAX];
    //匀速执行
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    //返回效果
    [UIView setAnimationRepeatAutoreverses:NO];
    frame.origin.x = -frame.size.width+100;
    runLabel.frame = frame;
    
    [UIView commitAnimations];
}

#pragma mark - 加载数据
/**
 加载网络图片轮播数据
 */
- (void)getloopData {
    
    //首页轮播图接口
    NSString *url = @"http://iosapi.itcast.cn/doctor/banners.json.php";
    NSDictionary *parameters = @{@"page_size":@10,@"page":@1};
    
    [[NetworkTool sharedTool] POSTWithURLStr:url parameters:parameters success:^(id responseObject) {
        
        NSDictionary *dic = responseObject[@"data"];
        NSArray *banners = dic[@"banners"];
        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:banners.count];
      
        //遍历数组
        for (NSDictionary *dict in banners) {
            
            [arrayM addObject:dict[@"banner_img_url"]];
            
        }
       //回到主线程,刷新UI
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            self.cycleScrollView.imageURLStringsGroup = arrayM;
        }];
        
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    //图片数组
    self.arr = [NSArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg", nil];
    //科室名称
    self.nameArr =[NSArray arrayWithObjects:@"肿瘤", @"心血管",@"神经科",@"血液科",@"骨科",@"公益活动",nil];
}

/**
 加载网络天气数据
 */
- (void)getWeatherData {
    
    //接口
    NSString *key = @"17IvzuqmKrK1cGwNL6VQebF9";
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    NSDictionary *parameters = @{@"location":@"广州",@"output":@"json",@"ak":key};
    
    [[NetworkTool sharedTool] GETWithURLStr:url parameters:parameters success:^(id responseObject) {
        
        NSArray *array= responseObject[@"results"];
        NSDictionary *dict= array[0];
        NSArray *weatherArray =dict[@"weather_data"];
        NSDictionary *weatherDict = weatherArray.firstObject;
        DHWeatherModel *weatherModel = [DHWeatherModel yy_modelWithDictionary:weatherDict];
        self.weatherView.weatherModel = weatherModel;
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
}

#pragma mark -UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    DHDoctorCollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.imageView.image = [UIImage imageNamed:self.arr[indexPath.item]];
    cell.label.text = self.nameArr[indexPath.row];
    return cell;
}

#pragma mark -UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.item == 5) {
    //推出公益活动界面
    DHPublicActivitiesViewController *publicActivitiesVC = [[DHPublicActivitiesViewController alloc] init];
        
        publicActivitiesVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:publicActivitiesVC animated:YES];
    } else {
    
    DHChooseDiseaseViewController *chooseDiseaseVC = [[DHChooseDiseaseViewController alloc] init];
        
    chooseDiseaseVC.diseaseType = self.nameArr[indexPath.item];
    chooseDiseaseVC.index = indexPath.item;
    
    chooseDiseaseVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController: chooseDiseaseVC animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    //开启侧滑
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    //关闭侧滑
    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    [self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeNone];
}

@end


















